/*引用库*/
$.evalFile(File($.fileName).parent + "/lib/kersBoru_lib.jsx");
$.evalFile(File($.fileName).parent + "/lib/json2.jsx");
try {
    JSON
} catch (e) {
    $.writeln("1因为未载入 JSON 解析库，请载入 json2.jsx ");
}



//var pat = "D:/Black8/bjj_cn/svn/ui/upload/T图标/";
//var excelFile = new File(pat + "配置表/英灵.json");
var configFile = new File("D:/Black8/bjj_cn/svn/ui/upload/T图标/配置表/索引_英灵卡.json");
/**
 * 
 * @returns 返回配置文件对象
 */
function configLine() {
    if (configFile.exists) {
        configFile.open("r"); //打开JSON文件
        var con_content = configFile.read(); //读取JSON内容
        var con_line = JSON.parse(con_content)[0];
    }
    return con_line;
}
/**
 * 主程序
 */
var main = function () {

    var con_line = configLine();
    var pat = con_line.local;
    var excelFile = new File(pat + con_line.json);

    if (excelFile.exists) {
        excelFile.open("r"); //打开JSON文件
        var content = excelFile.read(); //读取JSON内容
        var lines = JSON.parse(content);
        if (lines) {
            for (var i = 0; i < lines.length; i++) {

                var line = lines[i];
                var yl = analyticYL(line);
                var star = yl.star[0];
                var folderName = con_line.file;

                $.writeln("  ");
                if (line.isExport == "yes") {

                    if (yl.star.length >= 2) {

                        for (var j = 0; j < yl.star.length; j++) {

                            var star = yl.star[j];
                            var qualityValue = quality(star).value;
                            var qualityName = quality(star).name;
                            if (star == 13) {

                                for (var d = 0; d < 4; d++) {

                                    var dao = "星魂_" + [d];
                                    var star_ = star + d;
                                    var imgName = imageName(yl.id, star_);
                                    modifyPSDcontent(yl, qualityValue, qualityName, star, dao);
                                    saveImg(pat, folderName, imgName);
                                }

                            } else if (star == 14) {

                                for (var d = 0; d < 4; d++) {

                                    var dao = "星魂_" + [d];
                                    var star_ = star + 3 + d;
                                    var imgName = imageName(yl.id, star_);
                                    modifyPSDcontent(yl, qualityValue, qualityName, star, dao);
                                    saveImg(pat, folderName, imgName);
                                }

                            } else if (star == 15) {

                                for (var d = 0; d < 4; d++) {

                                    var dao = "星魂_" + [d];
                                    var star_ = star + 6 + d;
                                    var imgName = imageName(yl.id, star_);
                                    modifyPSDcontent(yl, qualityValue, qualityName, star, dao);
                                    saveImg(pat, folderName, imgName);
                                }

                            } else {
                                var dao = "星魂_0";

                                var imgName = imageName(yl.id, star);
                                modifyPSDcontent(yl, qualityValue, qualityName, star, dao);
                                saveImg(pat, folderName, imgName);
                            }




                        }
                    } else if (yl.star.length == 1) {
                        var star = yl.star[0];
                        var qualityValue = quality(star).value;
                        var qualityName = quality(star).name;
                        var dao = "星魂_0";
                        modifyPSDcontent(yl, qualityValue, qualityName, star, dao);

                        var imgName = imageName(yl.id, star);
                        saveImg(pat, folderName, imgName);

                    } else {
                        alert("星级数有问题");
                    }
                } else {
                    $.writeln(" 不导出  跳过 ");
                }



            }
        }
    }
    alert("结束");
}

try {
    var doc = app.activeDocument;
    main();
} catch (e) {
    alert("当前无激活的psd文档");
}


/**
 * 导出图片名字处理
 * @param {*} id 
 * @param {*} star 
 * @returns 
 */
function imageName(id, star) {
    var num = 10000 + id;
    if (star >= 10) {
        var imgName = num + "" + star;
    } else {
        var imgName = num + "0" + star;
    }

    return imgName;

}
/**
 * 创建一个新的英灵对象
 * @param {*} line 英灵数据
 * @returns 
 */
function analyticYL(line) {
    var YL = {};

    YL.name = line.name;
    YL.id = line.id;
    YL.star = [];
    YL.camp = "";
    YL.job = "";

    var maxStar = line.maxStar;
    var star = line.star;

    if (maxStar - star == 0) {

        YL.star[0] = star;

    } else {

        var len = maxStar - star + 1;
        //YL.star.length = len;
        YL.star[0] = star;
        for (var i = 1; i < len; i++) {

            YL.star[i] = YL.star[i - 1] + 1;
        }

    }
    if (line.camp) {

        switch (line.camp) {
            case 1:
                YL.camp = "风";
                break;
            case 2:
                YL.camp = "水";
                break;
            case 3:
                YL.camp = "火";
                break;
            case 4:
                YL.camp = "光";
                break;
            case 5:
                YL.camp = "暗";
                break;
            default:
                {
                    "错误";
                }
        }
    }
    if (line.job) {
        switch (line.job) {
            case 1:
                YL.job = "盾";
                break;
            case 2:
                YL.job = "战";
                break;
            case 3:
                YL.job = "法";
                break;
            case 4:
                YL.job = "牧";
                break;
            case 5:
                YL.job = "刺";
                break;
            default:
                {
                    "错误";
                }
        }
    }

    return YL;

}

/**
 * 
 * @param {*} yl 英灵对象
 * @param {*} qualityValue 当前英灵品质的颜色值
 * @param {*} qualityName  当前英灵品质 (绿,蓝,紫,橙,红,金)
 * @param {*} star 当前英灵星级
 * @param {*} dao  当前英灵道果(星魂)等级
 */
function modifyPSDcontent(yl, qualityValue, qualityName, star, dao) {

    var con_line = configLine();
    var layers = doc.layers;
    var pat_public = con_line.server;
    var imgPath = con_line.local + con_line.image + yl.id + ".png";
    var campPath = pat_public + con_line.camp + yl.camp + ".psd";
    var starPath = pat_public + con_line.star + star + ".psd";
    var daoguoPath = pat_public + con_line.dao + dao + ".psd";
    var quaPath = pat_public + con_line.quality + qualityName + ".psd";
    var jobPath = pat_public + con_line.job + yl.job + ".psd";
    //var job = yl.job;

    for (var i = 0; i < layers.length; i++) {
        var layer = layers[i];
        switch (layer.name) {
            case "图标资源": //替换 图标资源
                activeDocument.activeLayer = layer;
                kersBoru.listenerType.placedLayerReplaceContents(imgPath);
                break;
            case "左上角标": //替换 左上角标

                activeDocument.activeLayer = layer;
                kersBoru.listenerType.placedLayerReplaceContents(campPath);

                break;
            case "星级": //替换 星级
                if (layer.visible = false) {
                    layer.visible = true;
                    activeDocument.activeLayer = layer;
                    kersBoru.listenerType.placedLayerReplaceContents(starPath);
                } else {
                    activeDocument.activeLayer = layer;
                    kersBoru.listenerType.placedLayerReplaceContents(starPath);
                }
                break;
            case "道果": //替换 道果
                if (layer.visible = false) {
                    layer.visible = true;
                    activeDocument.activeLayer = layer;
                    kersBoru.listenerType.placedLayerReplaceContents(daoguoPath);
                } else {
                    activeDocument.activeLayer = layer;
                    kersBoru.listenerType.placedLayerReplaceContents(daoguoPath);
                }
                break;
            case "品质": //替换 品质框
                if (layer.visible = false) {
                    layer.visible = true;
                    activeDocument.activeLayer = layer;
                    kersBoru.listenerType.placedLayerReplaceContents(quaPath);
                } else {
                    activeDocument.activeLayer = layer;
                    kersBoru.listenerType.placedLayerReplaceContents(quaPath);
                }
            case "职名":
                if (layer.layers) {

                    var txt = layer.layers.getByName("txt");
                    var icon = layer.layers.getByName("职业");
                    var item = txt.textItem;
                    item.contents = yl.name;//修改文本内容(英灵名字)
                    item.color.rgb["hexValue"] = qualityValue;//修改文本颜色
                    activeDocument.activeLayer = icon;//选中图层
                    kersBoru.listenerType.placedLayerReplaceContents(jobPath);
                    activeDocument.activeLayer = layer;
                    kersBoru.listenerType.canvasConstituency("Al  ");
                    kersBoru.listenerType.alignDistribute("AdCH");
                    kersBoru.listenerType.canvasConstituency("None");
                    //$.writeln("未成功数量:");


                }

            default:
                { }
        }

    }



}
/**
 * 根据星级 获取英灵品质
 * @param {*} star 英灵当前星级
 * @returns 
 */
function quality(star) {
    var qua = {};
    if (star == 1) {
        qua.name = "绿";
        qua.value = "96e68c";
        return qua;
    }
    if (star == 3) {
        qua.name = "蓝";
        qua.value = "008ee0";
        return qua;
    }
    if (star == 4) {
        qua.name = "紫";
        qua.value = "da00d8";
        return qua;
    }
    if (star == 5) {
        qua.name = "橙";
        qua.value = "ff7700";
        return qua;
    }
    if (star > 5 & star < 11) {
        qua.name = "红";
        qua.value = "ea0101";
        return qua;
    }
    if (star > 10 & star < 16) {
        qua.name = "白";
        qua.value = "efe3ff";
        return qua;
    }
    qua.name = "无";
    qua.value = "无";
    return qua;

}
/**
 * 保存图片
 * @param {*} pat 链接对象的路径 目录
 * @param {*} type 图标类型
 * @param {*} imgName 图片名称
 */
function saveImg(pat, type, imgName) {

    var saveOption = new ExportOptionsSaveForWeb();
    saveOption.format = SaveDocumentType.PNG;
    saveOption.PNG8 = false;
    var path = pat + "正式名称1/" + type;
    var folder = new Folder(path);
    if (!folder.exists) {
        // alert("这里是folder.exists ");					//创建文件夹
        folder.create();
    }
    var outfile = new File(decodeURI(path + "/") + imgName + ".png"); //保存图片到
    //alert("这里是outfile   "+outfile);
    doc.exportDocument(outfile, ExportType.SAVEFORWEB, saveOption);
    //alert("这里是000000000000000   "); 
    $.writeln("保存");
}
